package com.mspbots.api.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.mspbots.api.service.TenantRuleService;
import com.mspbots.cw.request.EscalationDTO;
import com.mspbots.cw.request.TenantRuleDTO;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.List;
import java.util.Map;

import static java.time.temporal.ChronoUnit.SECONDS;

/**
 * com.mspbots.api.service.impl.TenantRuleServiceImpl
 *
 * @author Jun Zhou
 * @date 11:11 2019/7/1
 **/
@Deprecated
@Service
public class TenantRuleServiceImpl extends BaseGenericService implements TenantRuleService {

    @Override
    public List<TenantRuleDTO> list(Map<String, Object> params) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/rule/list-analysis", params);
        JsonNode result = resultMono.block(Duration.of(5, SECONDS));
        return this.objectMapper.convertValue(result, new TypeReference<List<TenantRuleDTO>>() {
        });
    }

    @Override
    public TenantRuleDTO getRule(Map<String, Object> params) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/rule/detail", params);
        JsonNode result = resultMono.block(Duration.of(5, SECONDS));
        return this.objectMapper.convertValue(result, TenantRuleDTO.class);
    }

    @Override
    public List<TenantRuleDTO> getEscalation(Map<String, Object> params) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/rule/getEscalation", params);
        JsonNode result = resultMono.block(Duration.of(5, SECONDS));
        return this.objectMapper.convertValue(result, new TypeReference<List<TenantRuleDTO>>() {
        });
    }

    @Override
    public List<EscalationDTO> selectEscalationParams(Map<String, Object> param) {
        Mono<JsonNode> resultMono = this.apiClient.get("/tenant/rule/selectEscalationParams", param);
        JsonNode result = resultMono.block(Duration.of(5, SECONDS));
        return this.objectMapper.convertValue(result, new TypeReference<List<EscalationDTO>>() {
        });
    }
}
